package com.main;

import com.lsj.trans.LANG;
import com.lsj.trans.exception.DupIdException;
import com.lsj.trans.factory.TFactory;
import com.lsj.trans.factory.TranslatorFactory;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.net.URISyntaxException;
import java.util.HashMap;


public class GoogleTranslateApplication extends Application {
    private Button btnTran;
    private Button btnSw;
    //    private RadioButton rbEtC;
//    private RadioButton rbCtE;
    private CheckBox cb;
    private TextArea taIn;
    private TextArea taOut;
    private Button btnTextInClear;
    private Button btnTextOutClear;
    private ComboBox<String> comboBox;
    private ComboBox<String> comboBoxSrcLang;
    private ComboBox<String> comboBoxTarLang;
    private HashMap<String,LANG> langMap = new HashMap<>();

    private TFactory factory = null;

    private void initFactory() {
        langMap.put("中",LANG.ZH);
        langMap.put("英",LANG.EN);
        langMap.put("日",LANG.JP);

        try {
            factory = new TranslatorFactory();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | DupIdException | URISyntaxException e) {
            //e.printStackTrace();
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("警告");
            alert.setHeaderText("发生异常");
            alert.setContentText("目前无法翻译！异常：" + e.getMessage());
            alert.showAndWait();
        }
    }

    @Override
    public void start(Stage primaryStage) {
        initFactory();
        //Scene主面板
        GridPane root = new GridPane();
        root.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        root.setPadding(new Insets(10));
        root.setHgap(10);
        root.setVgap(10);

        //各种组件
        //标签
        //Label lblG = new Label("谷歌翻译");
        // lblG.setStyle("-fx-text-fill: blue;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        comboBox = new ComboBox<>();
        comboBox.setStyle("-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        //comboBox.getItems().addAll("google","baidu","youdao","jinshan","tencent","omi");
        comboBox.getItems().addAll("google", "jinshan");
        comboBox.setValue("google");
        //两个按钮
        btnTran = new Button("翻译");
        btnTran.setStyle("-fx-text-fill: blue;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        btnSw = new Button("去掉换行");
        btnSw.setStyle("-fx-text-fill: green;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");

        //一个复选按钮
        cb = new CheckBox("翻译时去掉换行");
        cb.setStyle("-fx-text-fill: red;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");

//        //两个单选按钮
//        rbEtC = new RadioButton("英译汉");
//        rbEtC.setSelected(true);
//        rbCtE = new RadioButton("汉译英");
//        ToggleGroup group = new ToggleGroup();
//        rbEtC.setToggleGroup(group);
//        rbCtE.setToggleGroup(group);
//        rbEtC.setStyle("-fx-text-fill: black;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
//        rbCtE.setStyle("-fx-text-fill: black;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        comboBoxSrcLang = new ComboBox<>();
        comboBoxSrcLang.setStyle("-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        comboBoxSrcLang.getItems().addAll("英", "中", "日");
        comboBoxSrcLang.setValue("英");
        Label label = new Label("至");
        label.setStyle("-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        comboBoxTarLang = new ComboBox<>();
        comboBoxTarLang.setStyle("-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        comboBoxTarLang.getItems().addAll("英", "中", "日");
        comboBoxTarLang.setValue("中");
        //第一行的面板容器
        HBox hbTop1 = new HBox();
        hbTop1.setAlignment(Pos.CENTER);
        hbTop1.setSpacing(20);
        hbTop1.getChildren().addAll(comboBox, comboBoxSrcLang, label, comboBoxTarLang);

        HBox hbTop2 = new HBox();
        hbTop2.setAlignment(Pos.CENTER);
        hbTop2.setSpacing(20);
        hbTop2.getChildren().addAll(btnTran, btnSw, cb);
        //第二行的组件
        taIn = new TextArea();
        taIn.setWrapText(true);
        taIn.setStyle("-fx-font-size: 16px");
        taOut = new TextArea();
        taOut.setWrapText(true);
        taOut.setStyle("-fx-font-size: 16px");

        //为第二行的文本域添加表头
        Label lblTextIn = new Label("文本输入");
        lblTextIn.setStyle("-fx-text-fill: black;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        btnTextInClear = new Button("清除文本");
        btnTextInClear.setStyle("-fx-text-fill: black;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        HBox hBox1 = new HBox();
        hBox1.setAlignment(Pos.BOTTOM_LEFT);
        hBox1.setPadding(new Insets(5));
        hBox1.getChildren().addAll(lblTextIn);
        HBox hBox2 = new HBox();
        hBox2.setAlignment(Pos.TOP_RIGHT);
        hBox2.setPadding(new Insets(5));
        hBox2.getChildren().addAll(btnTextInClear);
        BorderPane borderPane1 = new BorderPane();
        borderPane1.setTop(hBox1);
        borderPane1.setBottom(hBox2);
        borderPane1.setCenter(taIn);

        Label lblTextOut = new Label("文本输出");
        lblTextOut.setStyle("-fx-text-fill: black;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        btnTextOutClear = new Button("清除文本");
        btnTextOutClear.setStyle("-fx-text-fill: black;-fx-font-family: 'Microsoft YaHei UI';-fx-font-size: 16px");
        HBox hBox3 = new HBox();
        hBox3.setAlignment(Pos.BOTTOM_LEFT);
        hBox3.setPadding(new Insets(5));
        hBox3.getChildren().addAll(lblTextOut);
        HBox hBox4 = new HBox();
        hBox4.setAlignment(Pos.TOP_RIGHT);
        hBox4.setPadding(new Insets(5));
        hBox4.getChildren().addAll(btnTextOutClear);
        BorderPane borderPane2 = new BorderPane();
        borderPane2.setTop(hBox3);
        borderPane2.setBottom(hBox4);
        borderPane2.setCenter(taOut);

        //在主面板中添加组件和容器
        root.add(hbTop1, 0, 0 );
        root.add(hbTop2, 1, 0 );
        root.addRow(1, borderPane1, borderPane2);
        GridPane.setHgrow(borderPane1, Priority.ALWAYS);
        GridPane.setVgrow(borderPane1, Priority.ALWAYS);
        GridPane.setHgrow(borderPane2, Priority.ALWAYS);
        GridPane.setVgrow(borderPane2, Priority.ALWAYS);
        //root.setGridLinesVisible(true);

        //添加动作
        addActionListener();
        //设置窗口stage
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.setTitle("谷歌翻译");
        primaryStage.show();
    }

    private void addActionListener() {
        btnTran.setOnAction(event -> {
            String textIn = taIn.getText();
            if (cb.isSelected()) {
                textIn = textIn.replaceAll("\n", " ");
            }
            try {
                String textOut = null;
                String translatorName = comboBox.getValue();
                String srcLang = comboBoxSrcLang.getValue();
                String tarLang = comboBoxTarLang.getValue();
                textOut = factory.get(translatorName).trans(langMap.get(srcLang), langMap.get(tarLang), textIn);

                taOut.setText(textOut);
            } catch (Exception e) {
                //e.printStackTrace();
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("警告");
                alert.setHeaderText("发生异常");
                alert.setContentText("翻译失败！异常：" + e.getMessage());
                alert.showAndWait();
            }
        });
        btnSw.setOnAction(event -> {
            String textIn = taIn.getText();
            String textOut = textIn.replaceAll("\n", " ");
            taOut.setText(textOut);
        });
        btnTextInClear.setOnAction(event -> taIn.setText(""));
        btnTextOutClear.setOnAction(event -> taOut.setText(""));
    }

    public static void main(String[] args) {
        launch(args);
    }
}
